package com.ailanyin.admin.modules.system.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.ailanyin.admin.modules.system.model.SysDictData;
import com.ailanyin.admin.modules.system.service.SysDictDataService;
import com.ailanyin.admin.modules.system.service.SysDictTypeService;
import com.ailanyin.common.result.AjaxResult;
import com.ailanyin.common.result.CommonPage;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统字典管理
 *
 * @author ailanyin
 * @version 1.0
 * @since 2021/10/15 0015 下午 17:02
 */
@RestController
@RequestMapping("/system/dict/data")
@Api(tags = "系统字典管理")
public class SysDictDataController {

    @Autowired
    private SysDictDataService sysDictDataService;
    @Autowired
    private SysDictTypeService sysDictTypeService;

    @PreAuthorize("hasAuthority('system:dict:list')")
    @GetMapping("/list")
    @ApiOperation(value = "获取字典列表")
    public CommonPage list(SysDictData dictData,
                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysDictData> list = sysDictDataService.selectDictDataList(dictData);
        return CommonPage.restPage(list);
    }

    @PreAuthorize("hasAuthority('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    @ApiOperation(value = "获取字典详情")
    public AjaxResult getInfo(@PathVariable Long dictCode) {
        return AjaxResult.success(sysDictDataService.selectDictDataById(dictCode));
    }

    @PreAuthorize("hasAuthority('system:dict:add')")
    @PostMapping
    @ApiOperation(value = "新增字典")
    public AjaxResult addDictData(@Validated @RequestBody SysDictData dict) {
        sysDictDataService.addDictData(dict);
        return AjaxResult.success();
    }

    @GetMapping(value = "/type/{dictType}")
    @ApiOperation(value = "根据字典类型查询字典数据信息")
    public AjaxResult dictType(@PathVariable String dictType) {
        List<SysDictData> data = sysDictTypeService.selectDictDataByType(dictType);
        if (CollectionUtil.isEmpty(data)) {
            data = new ArrayList<>();
        }
        return AjaxResult.success(data);
    }


    @PreAuthorize("hasAuthority('system:dict:edit')")
    @PutMapping
    @ApiOperation(value = "修改字典类型")
    public AjaxResult updateDictData(@Validated @RequestBody SysDictData dict) {
        sysDictDataService.updateDictData(dict);
        return AjaxResult.success();
    }

    @PreAuthorize("hasAuthority('system:dict:remove')")
    @DeleteMapping("/{dictCodes}")
    @ApiOperation(value = "批量删除字典类型")
    public AjaxResult remove(@PathVariable Long[] dictCodes) {
        sysDictDataService.deleteDictDataByIds(dictCodes);
        return AjaxResult.success();
    }
}
